package self.hoy.demo.cloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import self.hoy.demo.cloud.service.PaymentHystrixService;

import javax.annotation.Resource;

/**
 * @author 三共小不
 * @date 2020/6/4
 */
@RestController
@Slf4j
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @Value("${server.port}")
    private String port;


    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentOk(@PathVariable("id") Integer id) {
        return paymentHystrixService.paymentOk(id);
    }

    @HystrixCommand(
            fallbackMethod = "timeoutHandler",
            commandProperties = {
                    @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "3000")
            }
    )
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String timeout(@PathVariable("id") Integer id) {
        return paymentHystrixService.timeout(id);
    }

    public String timeoutHandler(@PathVariable("id") Integer id) {
        return "Wait too long in consumer " + port;
    }
}
